<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <script src="../js/vue-baidu-map.js"></script>


    <style>
        .map {
            width: 100%;
            height: 200px;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
    </style>
</head>
<body class="hold-transition">


<div id="app">
    <div class="content-header">
        <h1>预约管理
            <small>地址管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>地址管理</el-breadcrumb-item>
            <el-breadcrumb-item>地址项管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名称/体检机构" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="coordinate" label="经纬度" align="center"></el-table-column>
                <el-table-column label="机构图片" align="center">
                    <template slot-scope="scope">
                        <img :src="scope.row.img" alt="" style="width: 100px; height: 100px">
                    </template>
                </el-table-column>
                <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!--新增的编辑框-->
            <div class="add-form">
                <el-dialog title="地址管理项" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="手机号" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传体检机构地址图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/address/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                            <el-col :span="24">
                                <el-form-item label="经纬度" prop="coordinate">
                                    <el-input v-model="formData.coordinate"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址" prop="address">
                                    <el-input v-model="formData.address"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-button type="primary" icon="el-icon-location-outline" round
                                           @click="map(formData.name)">设置定位
                                </el-button>
                            </el-col>
                        </el-row>
                        <el-row>
                            <!--地图模块-->
                            <label><input v-model="keyword"></label>
                            <br>
                            <!-- getPoint方法，给地图加点击事件，点击地图获取所需要的信息，-->
                            <!--scroll-wheel-zoom属性是否可以用鼠标滚轮控制地图缩放-->
                            <baidu-map class="map" id="mapID" center="杭州市" :zoom="zoom" :scroll-wheel-zoom="true"
                                       @click="getPoint">
                                <!--地图类型，两种：一种是路线一种是绿的那种-->
                                <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
                                             anchor="BMAP_ANCHOR_TOP_LEFT"></bm-map-type>
                                <!--地图搜索功能，绑定上面的input，-->
                                <!--display: none样式很关键，因为下面默认会有地址提示信息很长，很烦，这样搜索会很舒服，-->
                                <!--zoom是搜索结果的视图比例，个人觉得12.8很舒服显示-->
                                <bm-local-search :keyword="keyword" :auto-viewport="true" :location="location" zoom="12.8"
                                                 style="display: none"></bm-local-search>
                                <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
                                <!--信息窗口，show属性是控制显示隐藏，infoWindowClose和infoWindowOpen是控制信息窗口关闭隐藏的方法-->
                                <bm-marker :position="postionMap">
                                    <bm-info-window :show="show" @close="infoWindowClose()" @open="infoWindowOpen()"
                                                    style="font-size: 14px">
                                        <p>站点：{{ add.siteName }}</p>
                                        <p>站点地址：{{ add.site }}</p>
                                    </bm-info-window>
                                </bm-marker>
                            </baidu-map>
                        </el-row>
                    </el-form>
                    <div>

                    </div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>

</body>
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../js/addressMap.js"></script>
</html>
<script>
    Vue.use(VueBaiduMap.default, {
        ak: '2y8uClQ3ZwQ2Ds2piG6G7ZtfHP4cNYVs'
    });

    var vue = new Vue({
        el: '#app',
        data: {
            jgNameDialog: false,
            show: false,
            postionMap: {  //地图坐标
                lng: 120.211486,
                lat: 30.256576
            },
            location: '',
            keyword: '',  //搜索框关键词
            zoom: 12.8,   //放大比例
            address: '',   //位置详细信息
            add: {
                siteName: '',
                site: '',
                jd: '',
                wd: '',
                desce: '',
                type: '',
                jgName: '',
                jgNum: '',
            },
            organizationData: [],
            jgName: '',
            jgNum: '',
            autoUpload: true,
            imageUrl: '',
            activeName: 'first',
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],
            formData: {
                coordinate: '',
                address:''
            },
            rules: {//校验规则
                name: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                telephone: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            },
            dialogFormVisible: false,

        },

        methods: {
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                //为模型数据imageUrl赋值，用于页面图片预览
                this.imageUrl = 'http://qvg9clowq.hd-bkt.clouddn.com/' + response.data;
                this.$message({
                    type: response.flag ? 'success' : 'error',
                    message: response.message
                });
                //设置模型数据（图片名称），后续提交ajax请求时会提交到后台最终保存到数据库
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },


            //分页查询
            handleCurrentChange() {
                //设置最新的页码
                this.pagination.currentPage = currentPage;
                //重新调用findPage方法进行分页查询
                this.findPage();
            },

            //分页查询
            findPage() {
                //console.log(this.pagination);
                axios.post('/address/findAll.do', this.pagination).then((resp) => {
                    if (resp.data.flag) {
                        //console.log(resp.data);
                        this.dataList = resp.data.data.rows;
                        this.pagination.total = resp.data.data.total;
                        //console.log(this.dataList);
                        //console.log(resp.data.rows);
                    } else {
                        this.$message.error({
                            message: resp.data.message
                        })
                    }
                })
            },

            //重置表单
            resetForm() {
                this.formData = {};//重置数据
                this.imageUrl = '';
            },
            //新建窗口弹出
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();
            },
            //添加地址信息
            handleAdd() {
                //判断数据是新增还是回显
                if (this.formData.id == null) {
                    //说明是新增
                    this.$refs['dataAddForm'].validate((valid) => {
                        if (valid) {
                            //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                            //console.log(this.formData);
                            axios.post("/address/add.do", this.formData).then((res) => {
                                //关闭新增窗口
                                this.dialogFormVisible = false;
                                if (res.data.flag) {//执行成功
                                    //弹出提示信息
                                    this.$message({
                                        message: res.data.message,
                                        type: 'success'
                                    });
                                } else {//执行失败
                                    //弹出提示
                                    this.$message.error(res.data.message);
                                }
                            }).finally(() => {
                                //新增成功后，重新调用分页查询方法，查询出最新的数据
                                this.findPage();
                                this.resetForm();
                            });
                        } else {
                            //校验不通过
                            this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                            return false;
                        }
                    });
                } else {
                    //没有id说明是编辑
                    this.$refs['dataAddForm'].validate((valid) => {
                        if (valid) {
                            //表单校验通过，发生ajax请求，将录入的数据提交到后台进行处理
                            //console.log(this.formData);
                            axios.post("/address/update.do", this.formData).then((res) => {
                                //关闭编辑窗口
                                this.dialogFormVisible = false;
                                //console.log(this.formData);
                                if (res.data.flag) {//执行成功
                                    //弹出提示信息
                                    this.$message({
                                        message: res.data.message,
                                        type: 'success'
                                    });
                                } else {//执行失败
                                    //弹出提示
                                    this.$message.error(res.data.message);
                                }
                            }).finally(() => {
                                //编辑成功后，重新调用分页查询方法，查询出最新的数据,并清空数据
                                this.findPage();
                                this.resetForm();
                            });
                        } else {
                            //校验不通过
                            this.$message.error("数据校验失败，请检查你的输入信息是否正确！");
                            return false;
                        }
                    });
                }


            },

            //编辑
            handleUpdate(row) {
                //弹出编辑框
                this.dialogFormVisible = true;
                //回显数据
                console.log(row)
                axios.get('/address/findById.do?id=' + row.id).then((resp) => {
                    if (resp.data.flag) {
                        this.formData = resp.data.data;
                        this.imageUrl = 'http://qvg9clowq.hd-bkt.clouddn.com/' + resp.data.data.img;
                    } else {
                        this.$message.error({
                            message: resp.data.message
                        })
                    }
                })
            },
            //删除
            handleDelete(row) {
                //根据id进行删除,先进行校验
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.get('/address/delete.do?id=' + row.id).then((resp) => {
                        if (resp.data.flag) {
                            this.$message({
                                type: 'success',
                                message: resp.data.message
                            })
                        } else {
                            this.$message.error({
                                message: resp.data.message
                            })
                        }
                    }).finally(() => {
                        this.findPage();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            map,
            getPoint,
            infoWindowClose,
            infoWindowOpen,
        },
        //钩子函数
        created() {
            //当页面加载完毕后,进行分页查询
            this.findPage();
        },

    });
</script>
